/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditorTemplatesChooser.java
 *
 * Created on 28/10/2010, 08:47:36
 */

package vnetlab.ui.editor;

import java.awt.BorderLayout;
import java.io.File;
import javax.swing.JFileChooser;
import vnetlab.manager.Configuration;

/**
 *
 * @author rogeriosv
 */
public class EditorTemplatesChooser extends javax.swing.JDialog {
    /** A return status code - returned if Cancel button has been pressed */
    public static final int RET_CANCEL = 0;
    /** A return status code - returned if OK button has been pressed */
    public static final int RET_OK = 1;

    /** Creates new form EditorTemplatesChooser */
    public EditorTemplatesChooser(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);

        jTextFieldRouter.setText(Configuration.getInstance().getRouterTemplate());
        jTextFieldComputer.setText(Configuration.getInstance().getComputerTemplate());
        jTextFieldServer.setText(Configuration.getInstance().getServerTemplate());
    }

    /** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
    public int getReturnStatus() {
        return returnStatus;
    }

    public String getRouterTemplate() {
         if(jTextFieldRouter.getText().replace(" ", "").equals(""))
            return null;
        else
            return jTextFieldRouter.getText();
    }

    public String getComputerTemplate() {
         if(jTextFieldComputer.getText().replace(" ", "").equals(""))
            return null;
        else
            return jTextFieldComputer.getText();
    }

    public String getServerTemplate() {
         if(jTextFieldServer.getText().replace(" ", "").equals(""))
            return null;
        else
            return jTextFieldServer.getText();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldRouter = new javax.swing.JTextField();
        jTextFieldComputer = new javax.swing.JTextField();
        jTextFieldServer = new javax.swing.JTextField();
        jButtonRouter = new javax.swing.JButton();
        jButtonServer = new javax.swing.JButton();
        jButtonComputer = new javax.swing.JButton();

        setTitle("Templates");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Router:");

        jLabel2.setText("Computer:");

        jLabel3.setText("Server:");

        jTextFieldRouter.setEditable(false);
        jTextFieldRouter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldRouterActionPerformed(evt);
            }
        });

        jTextFieldComputer.setEditable(false);

        jTextFieldServer.setEditable(false);

        jButtonRouter.setText("...");
        jButtonRouter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRouterActionPerformed(evt);
            }
        });

        jButtonServer.setText("...");
        jButtonServer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonServerActionPerformed(evt);
            }
        });

        jButtonComputer.setText("...");
        jButtonComputer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonComputerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextFieldServer)
                            .addComponent(jTextFieldComputer)
                            .addComponent(jTextFieldRouter, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cancelButton)
                        .addGap(6, 6, 6)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonRouter, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonComputer, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonServer, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldRouter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonRouter))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldComputer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonComputer))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldServer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonServer))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        doClose(RET_OK);
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        doClose(RET_CANCEL);
    }//GEN-LAST:event_cancelButtonActionPerformed

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        doClose(RET_CANCEL);
    }//GEN-LAST:event_closeDialog

    private void jTextFieldRouterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldRouterActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldRouterActionPerformed

    private void jButtonRouterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRouterActionPerformed
        JFileChooser chooser = new JFileChooser(new File(Configuration.getInstance().getTemplatesPath()));
        BorderLayout chooserLayout = (BorderLayout) chooser.getLayout();
        chooserLayout.getLayoutComponent(BorderLayout.NORTH).setVisible(false);
        chooser.getComponent(0).setVisible(false);
        chooser.setFileFilter(new DefaultFileFilter(".ovf", "VirtualBox export file"));
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION && chooser.getSelectedFile() != null) {
            jTextFieldRouter.setText(chooser.getSelectedFile().getName());
        }
    }//GEN-LAST:event_jButtonRouterActionPerformed

    private void jButtonComputerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonComputerActionPerformed
        JFileChooser chooser = new JFileChooser(new File(Configuration.getInstance().getTemplatesPath()));
        BorderLayout chooserLayout = (BorderLayout) chooser.getLayout();
        chooserLayout.getLayoutComponent(BorderLayout.NORTH).setVisible(false);
        chooser.getComponent(0).setVisible(false);
        chooser.setFileFilter(new DefaultFileFilter(".ovf", "VirtualBox export file"));
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION && chooser.getSelectedFile() != null) {
            jTextFieldComputer.setText(chooser.getSelectedFile().getName());
        }
    }//GEN-LAST:event_jButtonComputerActionPerformed

    private void jButtonServerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonServerActionPerformed
        JFileChooser chooser = new JFileChooser(new File(Configuration.getInstance().getTemplatesPath()));
        BorderLayout chooserLayout = (BorderLayout) chooser.getLayout();
        chooserLayout.getLayoutComponent(BorderLayout.NORTH).setVisible(false);
        chooser.getComponent(0).setVisible(false);
        chooser.setFileFilter(new DefaultFileFilter(".ovf", "VirtualBox export file"));
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION && chooser.getSelectedFile() != null) {
            jTextFieldServer.setText(chooser.getSelectedFile().getName());
        }
    }//GEN-LAST:event_jButtonServerActionPerformed

    private void doClose(int retStatus) {
        returnStatus = retStatus;
        setVisible(false);
        dispose();
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                EditorTemplatesChooser dialog = new EditorTemplatesChooser(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton jButtonComputer;
    private javax.swing.JButton jButtonRouter;
    private javax.swing.JButton jButtonServer;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextFieldComputer;
    private javax.swing.JTextField jTextFieldRouter;
    private javax.swing.JTextField jTextFieldServer;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables

    private int returnStatus = RET_CANCEL;

}
